package com.woniu.market_stall.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mysql.cj.Session;
import com.woniu.market_stall.entity.Stall;
import com.woniu.market_stall.mapper.LogMapper;
import com.woniu.market_stall.mapper.StallMapper;
import com.woniu.market_stall.vo.JwtInfo;
import com.woniu.market_stall.vo.LogVo;
import com.woniu.market_stall.vo.StallVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

@Service

public class StallService {

    @Autowired
    LogMapper logMapper;

    @Autowired
    StallMapper stallMapper;

    /**
     * 按照条件查询摊位
     * @param now 当前页面
     * @param size 页面大小
     * @param status 状态
     * @param marketId 市场id
     * @param stallName 摊位名字
     * @return 返回一个PageInfo
     */
    public PageInfo<StallVo> queryEmpByPage(Integer now , Integer size ,  String status , String marketId ,  String stallName){
        PageHelper.startPage(now , size);
        List<StallVo> list = stallMapper.selectStallByCons(status, marketId , stallName);

        return new PageInfo<>(list);
    }

    /**
     * 添加新的摊位
     * @param stallVo
     */
    @Transactional
    public void add(StallVo stallVo , int managerId ){
        LogVo l = new LogVo();
        l.setManagerId(managerId);
        l.setContent("执行了添加摊位的操作");
        l.setInsertTime(new Date());
        logMapper.addLog(l);
        stallMapper.insertStall(stallVo);
    }

    /**
     * 修改摊位信息
     * @param stallVo
     */
    @Transactional
    public void modify(StallVo stallVo , int managerId){
        LogVo l = new LogVo();
        l.setManagerId(managerId);
        l.setContent("执行了修改摊位的操作");
        l.setInsertTime(new Date());
        logMapper.addLog(l);
        stallMapper.updateStallById(stallVo);
    }

    /**
     * 删除摊位信息
     * @param stallId
     */
    public int del(int stallId){
        int i = stallMapper.selectStallStatusById(stallId);
        if (i == 0){
            stallMapper.deleteStallById(stallId);
            return 0;
        }else {
            return 1;
        }

    }

    /**
     * 查询所有未使用的摊位
     * @return
     */
   public List<Stall> queryAllUnUsedStall(){
        return stallMapper.selectAllUnUsedStall();
    }

    /**
     * 跟据摊位id查询所处市场id
     * @return
     */
    public int queryMarketIdByStallId(int stallId){
       return stallMapper.selectMarketIdByStallId(stallId);
    }

    /**
     * 查询所有竞拍中的摊位
     * @return
     */
    public List<Stall> queryAllAuctioningStall(){
        return stallMapper.selectAllAuctioningStall();
    }


    public List<Stall> queryAllStall() {
        return stallMapper.selectAll();
    }
}
